package com.itfreer.power.service.power.strategy;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.itfreer.form.dictionary.DictionaryManage;
import com.itfreer.form.dictionary.base.DictionaryItem;

/**
 * 应用服务权限数据提供接口 从字典缓存中获取，同时由字典缓存维护
 */
@Component("MemoryServiceJurisdictionDataProvide")
public class MemoryServiceJurisdictionDataProvide implements ServiceJurisdictionDataProvide {

	@Autowired
	private DictionaryManage dictionaryManage;

	private static final String serviceDic = "p_register_service";
	private static final String sysServiceDic = "p_sys_service";

	/**
	 * 获取系统可访问url地址集
	 * 
	 * @param sysid
	 *            系统ID值
	 * @param syspwd
	 *            系统访问码
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<String> getSysUrls(String sysid, String syspwd) {
		List<DictionaryItem> allService = dictionaryManage.getChild(serviceDic, "0", false);
		if (allService == null || allService.size() <= 0) {
			return null;
		}
		for (DictionaryItem item : allService) {
			if (item != null && item.getId().equals(sysid)) {
				if (item.getOther().get("ifprovide") != null && item.getOther().get("ifpower") != null) {
					String ifprovide = item.getOther().get("ifprovide").toString();
					if (ifprovide.equals(syspwd)) {
						String ifpower = item.getOther().get("ifpower").toString();
						String[] ids = ifpower.split(",");
						List<String> result = new ArrayList<String>();
						for (String id : ids) {
							DictionaryItem sDic = dictionaryManage.getDictionary(sysServiceDic, id);
							if (sDic != null) {
								result.addAll((List<String>) sDic.getOther().get("urls"));
							}
						}
						return result;
					}
				}
				return null;
			}
		}
		return null;
	}
}
